package com.example.androidgraphicfilter;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

	static String method;
	
	Paint mPaint;
	Path mPath;

	Bitmap mBitmap;

	// 디폴트 화이트
	int mBgColor = Color.WHITE;

	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public MyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	private void init(Context context) {
		mPaint = new Paint();
		mPaint.setColor(Color.RED);
		
		InputStream is = context.getResources().openRawResource(
				R.raw.gallery_photo_1);
		mBitmap = BitmapFactory.decodeStream(is);
		Bitmap scaleBitmap = Bitmap
				.createScaledBitmap(mBitmap, 100, 100, false);
		
		// 가장중요한것
		// 사용하지않는 비트맨만 날린다.
		mBitmap.recycle();
		mBitmap = scaleBitmap;
	}

	// 나만의 함수
	public void setBGColor(int color) {
		mBgColor = color;
		invalidate();
	}
	
	public void setSepia()
	{
		method = "sepia";
		invalidate();
	}
	
	public void setColorfilter()
	{
		method = "filter";
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		canvas.drawColor(mBgColor);
//		canvas.drawBitmap(mBitmap, 10, 10, mPaint);
		canvas.drawBitmap(mBitmap, 280, 400, mPaint);

		if(method == "sepia"){
			//ColorFilter - sepia
			ColorMatrix matrixA = new ColorMatrix();
			ColorMatrix matrixB = new ColorMatrix();
			
			matrixA.setSaturation(0);
			matrixB.setScale(1.0f, 0.95f, 0.85f, 1.0f);
			
			matrixA.setConcat(matrixB, matrixA);
			
			ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixA);
			mPaint.setColorFilter(filter);	
			canvas.drawBitmap(mBitmap, 280, 400, mPaint);
		}
		else if(method == "filter")
		{
			float[] m = {  -1, 0, 0, 0, 255,
							0, -1, 0, 0, 255, 
							0, 0, -1, 0, 255, 
							0, 0, 0, 1, 0};

			ColorMatrixColorFilter filter = new ColorMatrixColorFilter(m);
			mPaint.setColorFilter(filter);	
			canvas.drawBitmap(mBitmap, 280, 400, mPaint);
		}
	}
		
}
